﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace zevenseas.SharePoint.Solutions.FeatureBlocker.ApplicationPages
{
    public class CreateListPage : LayoutsPageBase
    {
        private const string PageToRedirectTo = "/_admin/zs.FeatureBlocker/settings.aspx";

        protected WebApplicationSelector uxWebApplicationSelector;
        protected InputFormTextBox uxListSite;
        protected InputFormTextBox uxListName;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }

        public override string PageToRedirectOnCancel
        {
            get
            {
                return PageToRedirectTo;
            }
        }

        public void OnClickOK(object sender, EventArgs e)
        {
            if (CreateList())
            {             
                SPUtility.Redirect(PageToRedirectTo, SPRedirectFlags.Default, this.Context);
            }
            else
            {
                SPUtility.TransferToErrorPage("List creation failed");
            }

        }
        
        public bool CreateList()
        {
            bool listIsCreated = false;

            using (SPSite site = new SPSite(uxListSite.Text))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    Guid guidList = web.Lists.Add(uxListName.Text, "List used to store feature requets", SPListTemplateType.GenericList);
                    SPList list = web.Lists[guidList];
                    list.Fields.Add("Reason", SPFieldType.Note, true);

                    SPFieldMultiLineText reasonField = (SPFieldMultiLineText)list.Fields["Reason"];
                    reasonField.ShowInViewForms = true;
                    reasonField.RichTextMode = SPRichTextMode.FullHtml;
                    reasonField.RichText = true;
                    reasonField.AllowHyperlink = true;
                    reasonField.Update();
                    
                    list.Fields.Add("Web", SPFieldType.Text, true);
                    list.Fields.Add("Url", SPFieldType.Text, true);
                    list.Update();

                    SPView defaultView = list.DefaultView;
                    defaultView.ViewFields.Add("Reason");
                    defaultView.ViewFields.Add("Web");
                    defaultView.ViewFields.Add("Url");
                    defaultView.Update();
                                        
                    SetSecurity(list);

                    SPWebApplication webApplication = uxWebApplicationSelector.CurrentItem;
                    FeatureBlockerConfig config = webApplication.GetChild<FeatureBlockerConfig>("FeatureBlockerConfig");
                    if (config == null)
                    {
                        config = new FeatureBlockerConfig("FeatureBlockerConfig", webApplication, Guid.NewGuid());
                        config.Update();
                    }

                    config.listUrl = list.ParentWeb.Url + "/" + list.RootFolder.Url;
                    config.Update();

                    listIsCreated = true;
                }
            }

            return listIsCreated;
        }

        private void SetSecurity(SPList list)
        {
            using (SPWeb web = list.ParentWeb)
            { 
                ////Create Custom Role Definitions
                //SPRoleDefinition roleDefinition = new SPRoleDefinition();
                //roleDefinition.Name = "EditPermissions for FeatureList";

               
                //using (SPWeb rootWeb = web.Site.RootWeb)
                //{
                //    if (rootWeb.RoleDefinitions[roleDefinition.Name] == null)
                //    {
                //        rootWeb.RoleDefinitions.Add(roleDefinition);
                //        rootWeb.Update();
                //    }

                //    roleDefinition = rootWeb.RoleDefinitions["EditPermissions for FeatureList"];
                //    roleDefinition.BasePermissions =
                //                                    SPBasePermissions.ViewListItems | SPBasePermissions.AddListItems |
                //                                    SPBasePermissions.BrowseDirectories | SPBasePermissions.BrowseUserInfo |
                //                                    SPBasePermissions.CreateAlerts;
                //    roleDefinition.Update();
                    
                //}

                SPRoleDefinition roleDefinition = web.RoleDefinitions["Contribute"];

                //Customize Idea Hopper List Security
                list.BreakRoleInheritance(false);
                
                SPRoleAssignment roleAssignment = new SPRoleAssignment(@"NT AUTHORITY\authenticated users", "", "", "");
                roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                list.RoleAssignments.Add(roleAssignment);

            }

            
        }
    }
}
